package com.ohuang.demo.http

import android.util.Log
import com.google.gson.Gson
import com.ohuang.demo.data.AAAA
import com.ohuang.demo.data.ClassName1
import com.ohuang.kthttp.HttpClient
import com.ohuang.kthttp.HttpRequest
import com.ohuang.kthttp.call.HttpCall
import com.ohuang.kthttp.call.map
import com.ohuang.kthttp.config.onStringBody
import com.ohuang.kthttp.get
import com.ohuang.kthttp.transform.transForm
import com.ohuang.kthttp.urlParams
import okhttp3.OkHttpClient

object testApi {
    var mOkHttpClient = OkHttpClient()
    var mHttpClient = HttpClient(mOkHttpClient, globalKtConfigCall = {
        onStringBody {

        }
    }, forceKtConfigCall = {
        urlParams(url){
           addParam("appid", "730")
        }
    })
    var gson = Gson()

    inline fun <reified T> request(noinline block: HttpRequest.() -> Unit): HttpCall<T> {
        return mHttpClient.httpCall<AAAA<T>>(gson.transForm(), block).map { it.data }
    }

    fun test(): HttpCall<ClassName1> {
        return mHttpClient.stringCall {
            get()
            url("http://192.168.2.85:8080/main/files/metaDataKey.json")
        }.map {
            Log.d("TAG", "test: $it")
            val transform = gson.transForm<AAAA<ClassName1>>().transform(it)
            return@map transform!!.data
        }
    }

    fun steamInfoHtml(): HttpCall<String> {
        return mHttpClient.stringCall {
            url("https://steamdb.info/app/730/")
        }
    }
}

